package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.bean.ResultBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ComplaintActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.flowlayout)
    TagFlowLayout flowlayout;
    @BindView(R.id.tv_content)
    EditText tvContent;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    private String orderId;
    private String status;
    private String content;
    private String ids="";
    private List<ResultBean.BodyBean> chooseBeans=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaint);
        ButterKnife.bind(this);
        initView();
        getDatas();
    }

    private void getDatas() {
        OkgoUtil.post(Http.GetTextLabel,ComplaintActivity.this)
                .params("texttype","2")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ResultBean parentBean= DealResult.getInstance().dealDatas(ComplaintActivity.this,response,new TypeToken<ResultBean>() {}.getType());
                        if(parentBean==null){return;}
                        int code=parentBean.getCode();
                        if(code== Constant.SUCESS||code==Constant.EMPTY){
                            setDatas(parentBean.getBody());
                        }else {
                            ToastUtils.toastForShort(ComplaintActivity.this,parentBean.getResult());
                        }
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.toastForShort(ComplaintActivity.this,ComplaintActivity.this.getString(R.string.bad_net));
                    }
                });
    }

    private void setDatas(List<ResultBean.BodyBean> body) {
        if(body.isEmpty()){
            return;
        }
        chooseBeans.addAll(body);
//        flowlayout.setMaxSelectCount(1);
        TagAdapter<ResultBean.BodyBean> tagAdapter = new TagAdapter<ResultBean.BodyBean>(body) {
            @Override
            public View getView(FlowLayout parent, int position, ResultBean.BodyBean labelBean) {
                String tip = labelBean.getTextinfo();
                TextView textView = (TextView) LayoutInflater.from(ComplaintActivity.this)
                        .inflate(R.layout.tv_label_red_select, flowlayout, false);
                textView.setText(tip);
                textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
                return textView;
            }
        };
        flowlayout.setAdapter(tagAdapter);
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        Intent i=getIntent();
        String title=i.getStringExtra("name");
        tvMiddleTitle.setText(title);
        status= i.getStringExtra("status");
        orderId=i.getStringExtra("id");
        if(title.contains("投诉")){
            tvContent.setHint("请输入您投诉的理由");
        }else{
            tvContent.setHint("请输入您拒绝的理由");
        }
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_confirm:
                content = tvContent.getText().toString();
                if(!chooseBeans.isEmpty()){
                    Set<Integer> sets = flowlayout.getSelectedList();
                    for(Integer i:sets){
                        ids=ids+chooseBeans.get(i).getId()+",";
                    }
                    if(ids.endsWith(",")){
                        ids=ids.substring(0,ids.length()-1);
                    }
                }
                if(TextUtils.isEmpty(content)&&TextUtils.isEmpty(ids)){
                    ToastUtils.toastForShort(this,"标签或者理由不能同时为空");
                    return;
                }
                confirm();
                break;
        }
    }

    private void confirm() {
        OkgoUtil.post(Http.HandleOrder,ComplaintActivity.this)
                .params("content",content)
                .params("orderid",orderId)
                .params("status",status)
                .params("idStr",ids)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        BaseBean parentBean= DealResult.getInstance().dealDatas(ComplaintActivity.this,response,new TypeToken<BaseBean>() {}.getType());
                        if(parentBean==null){return;}
                        int code=parentBean.getCode();
                        if(code== Constant.SUCESS){
                            ComplaintActivity.this.finish();
                        }else {
                        }
                        ToastUtils.toastForShort(ComplaintActivity.this,parentBean.getResult());
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.toastForShort(ComplaintActivity.this,ComplaintActivity.this.getString(R.string.bad_net));
                    }
                });
    }
}
